﻿#include "DAHoliday.h"

#include <QPainter>
#include <QPen>
#include <QRect>
#include <QTimer>
#include <QVBoxLayout>

DAHoliday::DAHoliday(QWidget *parent)
    : QWidget{parent}
{
    setObjectName("dah");
    Title.setParent(this);
    Title.setObjectName("dahTitle");
    Title.setAlignment(Qt::AlignCenter);
    Title.setText(setting.GetStringValue(HLD_TITLE));

    Time.setParent(this);
    Time.setObjectName("dahTime");
    Time.setAlignment(Qt::AlignCenter);
    QTimer *timer = new QTimer(this);

    QObject::connect(timer,&QTimer::timeout,[=]()
    {
        Time.setText(LeaveTime(setting.GetStringValue(HLD_DATE)));
    });
    timer->start(1000);
    QVBoxLayout *vb = new QVBoxLayout();
    vb->addWidget(&Title);
    vb->addWidget(&Time);
    vb->setSpacing(0);
    vb->setContentsMargins(0,0,0,0);//方便兼容qt6

    setLayout(vb);
}
void DAHoliday::paintEvent(QPaintEvent *e)
{
    //创建绘画对象
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing); // 反锯齿;
    painter.setBrush(QBrush(QColor(0, 0, 0, 50)));
    painter.setPen(Qt::transparent);

    QRect rect = this->rect();
    rect.setWidth(rect.width()-1);
    rect.setHeight(rect.height()-1);
    painter.drawRoundedRect(rect,5,5);

//    QPen pen;
//    pen.setColor(Qt::red);
//    pen.setWidth(3);
//    painter.setPen(pen);
//    painter.drawRect(this->rect());
//    painter.end();
    QWidget::paintEvent(e);
}
QString DAHoliday::LeaveTime(QString Date,QString Format)
{
    QDateTime time = QDateTime::currentDateTime();
    QDateTime time1 = QDateTime::fromString(Date,Format);
    qint64 secs;
    if(time1 > time)
        secs = time.secsTo(time1);
    else
        secs = time1.secsTo(time);
    QTime t(0,0,0);
    t = t.addSecs(secs);
    qint64 days = secs/60/60/24;
    QString sd;
    QString s;
    if (days != 0)
        sd += QString::number(days) + " 天";
    QString stip = "";
    if(t.toString("h") != "0")
    {
        sd += t.toString(" h 时 m 分 s 秒");
    }
    else
    {
        sd += t.toString(" m 分 s 秒");
    }

    if (time1 > time)
    {
        s.append(tr("还有"));
        stip = s + sd;
    }
    else
    {
        s.append(tr("已经"));
        stip = s+sd+"了";
    }
   return stip;
}
